package phone.qyg.com.qyg.utils;

import android.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.RecyclerViewItemDecoration;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.SharePrefUtil;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Base;


/**
 * 作者：lblbh on 2017/12/5 09:53
 * 邮箱：lanbuhan@163.com
 * 功能：TODO
 */
public class PopuTypeSelectState  {

    public static  void showDialog(final android.content.Context context, final int type, final OnAlertOnClick onAlertOnClick){
        String result = SharePrefUtil.getString(SharePrefUtil.KEY.APP_INFO,"");
        String key = "";
        switch (type){
            case ApplicationConfig.MARITAL:
                key = JSONTool.Enum.MARITAL_TYPE;
                break;
            case ApplicationConfig.EDU_LEVEL:
                key = JSONTool.Enum.EDULEVEL_TYPE;
                break;
            case ApplicationConfig.INDUSTRY:
                key = JSONTool.Enum.INDUSTRY_TYPE;
                break;
            case ApplicationConfig.PRO_BELM_TYPE:
                key = JSONTool.Enum.PRO_BELM_TYPE;
                break;

        }
        final String keyFinal = key;
        if(CommonUtil.isRequestStr(result)){
            String value = JSONTool.jsonDefaluTranString(result,key);
            setShowData(context,value,type,onAlertOnClick);
        }else{
            AppHttpRequest.httpAppInfo(new AppHttpRequest.OnAppHttpListener() {
                @Override
                public void onAppHttpState(boolean isState, String result) {
                    String value = JSONTool.jsonDefaluTranString(result,keyFinal);
                    setShowData(context,value,type,onAlertOnClick);
                }
            });
        }
    }


    /**
     * 设置popu弹出窗体数据
     */
    public static void setShowData(android.content.Context content, String result, final int dataType, final OnAlertOnClick onAlertOnClick){
        if(!CommonUtil.isRequestStr(result))
            return;
        List<Base> themes = JSONTool.jsonTransformClazzs(result, Base.class);
        LayoutInflater inflatersex = LayoutInflater.from(QXApplication.getContext());
        View view = inflatersex.inflate(R.layout.alert_dialog, null);
        final AlertDialog showAlert = new AlertDialog.Builder(content).create();
        showAlert.show();
        showAlert.getWindow().setContentView(view);
        RecyclerView recycler = (RecyclerView) view.findViewById(R.id.recycler);

        //设置布局管理器
        LinearLayoutManager layoutVerManager = new LinearLayoutManager(QXApplication.getContext());
        //设置竖向
        layoutVerManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.addItemDecoration(new RecyclerViewItemDecoration(
                RecyclerViewItemDecoration.MODE_HORIZONTAL,
                QXApplication.getContext().getResources().getColor(R.color.divider),
                CommonUtil.dip2px(QXApplication.getContext(),1),0,0));

        recycler.setLayoutManager(layoutVerManager);
        final CommonAdapter adapter = new CommonAdapter<Base>(QXApplication.getContext(),R.layout.item_alert,themes) {
            @Override
            protected void convert(ViewHolder holder, Base  theme, int position) {

                holder.setText(R.id.item_title,theme.title);
                if(theme.type != 1 && theme.type != 2){
                    holder.setTextColorRes(R.id.item_title,R.color.login_fill_btn);
                }else{
                    holder.setTextColorRes(R.id.item_title,R.color.black);
                }
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Base theme = (Base) adapter.getData().get(position);
                if(onAlertOnClick != null)
                    onAlertOnClick.onClick(theme,dataType,showAlert);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recycler.setAdapter(adapter);
    }

    /**
     * 设置popu弹出窗体数据
     */
    public static void setShowData(android.content.Context content, List<Base> result, final OnAlertOnClick onAlertOnClick){
        if(result==null)
            return;
        LayoutInflater inflatersex = LayoutInflater.from(QXApplication.getContext());
        View view = inflatersex.inflate(R.layout.alert_dialog, null);
        final AlertDialog showAlert = new AlertDialog.Builder(content).create();
        showAlert.show();
        showAlert.getWindow().setContentView(view);
        RecyclerView recycler = (RecyclerView) view.findViewById(R.id.recycler);

        //设置布局管理器
        LinearLayoutManager layoutVerManager = new LinearLayoutManager(QXApplication.getContext());
        //设置竖向
        layoutVerManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.addItemDecoration(new RecyclerViewItemDecoration(
                RecyclerViewItemDecoration.MODE_HORIZONTAL,
                QXApplication.getContext().getResources().getColor(R.color.divider),
                CommonUtil.dip2px(QXApplication.getContext(),1),0,0));

        recycler.setLayoutManager(layoutVerManager);
        final CommonAdapter adapter = new CommonAdapter<Base>(QXApplication.getContext(),R.layout.item_alert,result) {
            @Override
            protected void convert(ViewHolder holder, Base  theme, int position) {

                holder.setText(R.id.item_title,theme.title);
                if(theme.type != 1 && theme.type != 2){
                    holder.setTextColorRes(R.id.item_title,R.color.login_fill_btn);
                }else{
                    holder.setTextColorRes(R.id.item_title,R.color.black);
                }
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Base theme = (Base) adapter.getData().get(position);
                if(onAlertOnClick != null)
                    onAlertOnClick.onClick(theme,0,showAlert);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recycler.setAdapter(adapter);
    }

    /**
     * 设置alert 回调
     */
    public interface OnAlertOnClick{
        /**
         *
         * @param theme 单条数据
         * @param dataType 数据类型
         */
        public void onClick(Base theme, int dataType, AlertDialog showAlert);
    }
}
